package cn.edu.tsinghua.ie.ieFunds.controller;

import cn.edu.tsinghua.ie.ieFunds.bean.bussiness.Transaction;
import cn.edu.tsinghua.ie.ieFunds.service.TransactionService;
import cn.edu.tsinghua.ie.ieFunds.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("transaction")
public class TransactionController {

    @Autowired
    private UserService userService;

    @Autowired
    private TransactionService transactionService;

    @RequestMapping("getTransactionList")
    public List<Transaction> getTransactionList() {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String username = userDetails.getUsername();
        return transactionService.getAllByUsername(userService.getUserByUsername(username));
    }
}
